# -*- coding: utf-8 -*-
'''
Created on 25.04.2009

@author: renat
'''


    

from process import process
from process import inf
from sets import Set
import pickle
import os

def r1(self):
    return self.node == 1
def s1 (self):
    return self.node == 0 or self.node == 2
inf.recieved = r1
inf.sent = s1

rates =   ['0.95','1.102',
         #'1.201',
          '1.27',
         #'1.295',
         #'1.305',
         '1.322',
         #'1.34', 
         '1.41','1.51', '1.55', '1.61', '1.75', '1.9','2.1', '2.25','2.5']
#, '2.5', '3','4','5']
resr = {}
allkeys =Set()

filKeys = Set(['0 SNT AGT', '2 SNT AGT' , '0 SNT RTR', '2 SNT RTR' ,'1 RCV MAC', '0 RCV RTR', '2 RCV RTR', '2 ARP' ])
descr = {
         '1 RCV AGT' : 'Всего приято данных',
         "1 COL":"Интенсивность коллизий",
         '0 SNT MAC':'Отправлено на 1 узле на MAC уровне',
         '2 SNT MAC':'Отправлено на 2 узле на MAC уровне',
         '0 RET':'Потери - превышено число повторов на 1м',
         '2 RET':'Потери - превышено число повторов на 2м',
         '0 n1:IFQ':'Потери - переполнение очереди интерфейса на 1м',
         '2 n1:IFQ':'Потери - переполнение очереди интерфейса на 2м'
         }
cts = ''
for rate in rates:
    print rate, "start"
    if not os.path.exists("../raw/test"+cts+"."+rate+"Mb"):
        os.system("cd .. ; ~/ns-allinone-2.33/ns/ns ./3.tcl "+rate+"Mb "+cts)
    if os.path.exists("../data/rate"+cts+rate):
        f = open("../data/rate"+cts+rate, "r")
        resr[rate] = pickle.load(f)
        f.close()
        print "rate loaded"
    else:
        f = open("../raw/test"+cts+"."+rate+"Mb","rb")
        proc = process(3)
        proc.mode = "a"
        proc.dct = 20
        proc.open()
        proc.parse(f);
        proc.close();
        resr[rate] = proc.pack_stats
        fpi = open("../data/rate"+cts+rate, "wb")
        pickle.dump(proc.pack_stats,fpi)
        fpi.close()
        print "rate generated"
    allkeys =  allkeys | Set(resr[rate].iterkeys())
    print rate, "done"
    

    

drf = open("../data/dropreason_hid"+cts,'w')
drf.write("#\t");
for key1 in allkeys:
    if not key1 in filKeys:
        drf.write(key1+"\t")
drf.write("\n");
for rate in rates:
    map = resr[rate]
    drf.write(rate+"\t")
    for key in allkeys:
        if not key in filKeys:
            if key in map:
                drf.write(`map[key]*8.0/15000`+"\t")
            else:
                drf.write("0\t")
    drf.write("\n")
drf.close()

splot = None
i =2 
for key in allkeys:
    if not key in filKeys:
        if splot:
            splot += ",'../data/dropreason_hid"+cts+"' using 1:"+`i`+" title '"+descr[key]+"' with lines"
        else:
            splot = "'../data/dropreason_hid"+cts+"' using 1:"+`i`+" title '"+descr[key]+"' with lines"
        i+=1

#psc = open("a.plot","w")
psc = os.popen("gnuplot", "w")

psc.write("set terminal png size 900,800 font '/usr/share/fonts/truetype/FreeSans.ttf' 12\n"+ 
"set encoding utf8 \n"+
"set xlabel 'Мб/c' \n"+
"set ylabel 'Мб/c' \n"+
"set key outside center bottom \n"+
"set output '../graphs/overall-hid"+cts+".png'\n" +  
"plot " + splot + "\n")
psc.close()
